package com.jlcindia.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.jlcindia.hibernate.Candidate;
import com.jlcindia.util.CHibernateTemplate;
import com.jlcindia.util.CHibernateUtil;

public class HibernateCandidateDAO implements CandidateDAO {

	@Override
	public void addCandidate(Candidate can) {
		CHibernateTemplate.save(can);
	}

	@Override
	public void updateCandidate(Candidate can) {
		CHibernateTemplate.update(can);
		
	}

	@Override
	public void deleteCandidate(int cid) {
		CHibernateTemplate.delete(Candidate.class,cid);
		
	}

	@Override
	public Candidate getCandidateByCid(int cid) {
		Candidate can = (Candidate)CHibernateTemplate.load(Candidate.class,cid); 
		return can;
	}

	@Override
	public List<Candidate> getAllCandidates() {
		List<Candidate> cans = new ArrayList<Candidate>();
		try {
			SessionFactory sf = CHibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			Transaction tx = session.beginTransaction();
			String HQL = "from Candidate can";
			Query q = session.createQuery(HQL);
			cans = q.list();
			tx.commit();
			session.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cans;
	}

}
